layui.use(['form', 'layer', 'table', 'ax', 'func'], function () {
    var layer = layui.layer;
    var table = layui.table;
    var $ax = layui.ax;
    var func = layui.func;
    var form = layui.form;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    /**
     * 初始化表格
     */
    var User = {
        tableId: "UserTable" //表格id
    };

    /**
     * 初始化表格的列
     */
    User.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'userName', align: "center", sort: true, title: '用户名'},
            {field: 'avatar', align: "center", sort: true, title: '头像',
                templet: function(object){
                    if('' == object.avatar || null == object.avatar || undefined == object.avatar){
                        return '-';
                    }else{
                        return '<img src="' + object.avatar + '" style="width:40px; height:40px;">';
                    }
                }
            },
            {field: 'gender', align: "center", sort: true, title: "性别",
                templet: function(object){
                    var html = '';
                    if(0 == object.gender){
                        html += '<span class="layui-badge">保密</span>';
                    }else if(1 == object.gender){
                        html += '<span class="layui-badge layui-bg-green">男</span>';
                    }else if(2 == object.gender){
                        html += '<span class="layui-badge layui-bg-blue">女</span>';
                    }
                    return html;
                }
            },
            {field: 'phone', align: "center", sort: true, title: "手机号",
                templet: function(object){
                    if('' == object.phone || null == object.phone || undefined == object.phone){
                        return '-';
                    }else{
                        return object.phone;
                    }
                }
            },
            {field: 'province', align: "center", sort: true, title: '省份',
                templet: function(object){
                    if('' == object.province || null == object.province || undefined == object.province){
                        return '-';
                    }else{
                        return object.province;
                    }
                }
            },
            {field: 'city', align: "center", sort: true, title: '城市',
                templet: function(object){
                    if('' == object.city || null == object.city || undefined == object.city){
                        return '-';
                    }else{
                        return object.city;
                    }
                }
            },
            {field: 'town', align: "center", sort: true, title: '区/镇',
                templet: function(object){
                    if('' == object.town || null == object.town || undefined == object.town){
                        return '-';
                    }else{
                        return object.town;
                    }
                }
            },
            {field: 'address', align: "center", sort: true, title: '详细地址',
                templet: function(object){
                    if('' == object.address || null == object.address || undefined == object.address){
                        return '-';
                    }else{
                        return object.address;
                    }
                }
            },
            {field: 'gold', align: "center", sort: true, title: '金币值'},
            {field: 'updateDate', align: "center", sort: true, title: '更新时间'},
            {field: 'updateByName', align: "center", sort: true, title: '更新者'},
            {align: 'center', toolbar: '#tableBar', title: "操作", minWidth: 200}
        ]];
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + User.tableId,
        url: Feng.ctxPath + '/web/user/list',
        page: true,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        height: "full-98",
        cellMinWidth: 100,
        cols: User.initColumn()
    });

    /**
     * 点击查询按钮
     */
    User.search = function () {
        var queryData = {};
        queryData['userName'] = $("#userName").val();
        queryData['phone'] = $("#phone").val();
        queryData['gender'] = $("#gender").val();
        table.reload(User.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 点击重置按钮
     */
    User.reset = function () {
        $("#userName").val("");
        $("#phone").val("");
        $("#gender").val("-1");

        form.render();

        var queryData = {};
        queryData['userName'] = "";
        queryData['phone'] = "";
        queryData['gender'] = "-1";
        User.search();
    };

    /**
     * 弹出添加对话框
     */
    User.openAdd = function () {
        func.open({
            title: "添加用户",
            maxmin: true,
            height: 500,
            content: Feng.ctxPath + '/web/user/addOrEdit',
            tableId: User.tableId
        });
    };

    /**
     * 点击编辑按钮时
     *
     * @param data 点击按钮时候的行数据
     */
    User.onEdit = function (data) {
        func.open({
            title: "编辑用户(" + data.userName + ")",
            maxmin: true,
            height: 500,
            content: Feng.ctxPath + '/web/user/addOrEdit?userId=' + data.id,
            tableId: User.tableId
        });
    };

    /**
     * 点击修改金币按钮时
     *
     * @param data 点击按钮时候的行数据
     */
    User.onUpdateGold = function (data) {
        func.open({
            title: "修改用户金币(" + data.userName + ")",
            maxmin: true,
            height: 550,
            width: '550px',
            content: Feng.ctxPath + '/web/user/updateGoldIndex?userId=' + data.id,
            tableId: User.tableId
        });
    };

    /**
     * 点击删除按钮
     *
     * @param data 点击按钮时候的行数据
     */
    User.onDelete = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/web/user/delete", function (data) {
                Feng.success("删除成功");
                table.reload(User.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("userId", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除用户(" + data.userName + ")?", operation);
    };

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        User.search();
    });

    // 重置按钮点击事件
    $('#btnReset').click(function () {
        User.reset();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        User.openAdd();
    });

    // 工具条点击事件
    table.on('tool(' + User.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            User.onEdit(data);
        } else if (layEvent === 'delete') {
            User.onDelete(data);
        } else if (layEvent === 'updateGold') {
            User.onUpdateGold(data);
        }
    });

});

$(function () {
    var panehHidden = false;
    if ($(this).width() < 769) {
        panehHidden = true;
    }
    $('#myContiner').layout({initClosed: panehHidden, west__size: 260});
});